package com.itheima.social.securitys.controller;
import com.itheima.common.controller.BaseController;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import com.itheima.social.securitys.client.EmployeeFeginClient;
import com.itheima.social.securitys.pojo.Archive;
import com.itheima.social.securitys.pojo.ArchiveDetail;
import com.itheima.social.securitys.pojo.CompanySettings;
import com.itheima.social.securitys.pojo.UserSocialSecurity;
import com.itheima.social.securitys.pojo.vo.User;
import com.itheima.social.securitys.service.ArchiveService;
import com.itheima.social.securitys.service.CompanySettingsService;
import com.itheima.social.securitys.service.PaymentItemService;
import com.itheima.social.securitys.service.UserSocialService;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/6 18:08
 */
@CrossOrigin
@RestController
@RequestMapping("/social_securitys")
public class SocialSecurityController extends BaseController {

    @Autowired
    private CompanySettingsService companySettingsService;

    @Autowired
    private UserSocialService userSocialService;


    @Autowired
    private PaymentItemService paymentItemService;

    @Autowired
    private ArchiveService archiveService;


    /**
     * 查询企业是否设置过社保
     */
    @GetMapping(value = "/settings")
    public Result settings() {
       CompanySettings companySettings= companySettingsService.findById(this.companyId);
       return new Result(ResultCode.SUCCESS,companySettings);
    }

    /**
     * 设置企业社保
     */
    @PostMapping(value = "/settings")
    public Result saveSettings(@RequestBody CompanySettings companySettings){
        companySettings.setCompanyId(this.companyId);
        Integer count=companySettingsService.saveSettings(companySettings);
        return count>1?new Result(ResultCode.SUCCESS):new Result(ResultCode.FAIL);
    }


    /**
     * 新建报表
     */
    @PutMapping("/historys/{yearMonth}/newReport")
    public Result saveNewReportSettings(@PathVariable("yearMonth") String yearMonth){
        try{
            companySettingsService.saveNewReportSettings(yearMonth,companyId);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 查询企业员工的社保信息列表
     */
    @PostMapping("/list")
        public Result list(@RequestBody Map map){
        //获取页面
        Integer page= (Integer) map.get("page");
        //获取每页显示的数据量
        Integer pageSize= (Integer) map.get("pageSize");
        return new Result(ResultCode.SUCCESS,userSocialService.list(page,pageSize,this.companyId));
    }



    /**
     * 根据用户id查询用户的社保数据
     *
     * 1. 根据用户id查询用户数据
     * 2. 根据用户id查询社保数据
     */
    @GetMapping("/{userId}")
    public Result findUserSocialByUserId(@PathVariable("userId") String userId){
        Map<String,Object> resultMap=new HashMap<String,Object>(0);
        //查询用户数据
        User userClient = userSocialService.findUserPersonalByUserId(userId);
        resultMap.put("user",userClient);
        //根据用户id查询社保数据
        UserSocialSecurity userSocialSecurity=userSocialService.findUserSocialByUserId(userId);
        resultMap.put("uss",userSocialSecurity);
        return new Result(ResultCode.SUCCESS,resultMap);
    }

    /**
     * 根据城市id查询参保城市的参保项目
     */
    @GetMapping("/payment_item/{cityId}")
    public Result findPaymentItemByCityId(@PathVariable("cityId") String cityId){
        return new Result(ResultCode.SUCCESS,paymentItemService.findCityPaymentItemListByCityId(cityId));
    }

    /**
     * 保存更新用户社保
     */
    @PutMapping
    public Result saveUserSocialSecurity(@RequestBody UserSocialSecurity userSocialSecurity){
        Integer rows=userSocialService.saveUserSocialSecurity(userSocialSecurity);
        return rows>0?new Result(ResultCode.SUCCESS):new Result(ResultCode.FAIL);
    }

    /**
     * 查询月份数据报表
     *   /historys/2019/07?yearMonth=201907&opType=1
     *
     *   opType :1  当月
     *          : 其他（历史归档详情记录）
     */

    @Autowired
    private EmployeeFeginClient client;

    @GetMapping("/historys/{yearMonth}")
    public Result historys(@RequestParam("yearMonth") String yearMonth,@RequestParam("opType") Integer opType) throws Exception {
        List<ArchiveDetail> archiveDetailList=new ArrayList<ArchiveDetail>(0);
        if(opType==1){
            //当月，未归档的
            archiveDetailList=archiveService.getReports(companyId,yearMonth);
        }else{
            //不是当月
            //根据企业id和年月查询是否存在归档，如果存在，查询归档明细
            Archive archive=archiveService.findArchiveByCompanyIdAndYearMonth(this.companyId,yearMonth);
            if(archive!=null){
                //根据归档历史记录id查询归档历史明细
                archiveDetailList=archiveService.findArchiveDetailByArchiveId(archive.getId());
            }
        }
        return new Result(ResultCode.SUCCESS,archiveDetailList);
    }

    /**
     * 设置归档
     * /historys/{yearMonth}/archive
     */
    @PostMapping("/historys/{yearMonth}/archive")
    public Result setArchvie(@RequestParam("yearMonth") String yearMonth){
        try {
            archiveService.setArchive(yearMonth, this.companyId);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 查询某年内社保归档列表
     * /historys/2019/list
     */
    @GetMapping("/historys/{year}/list")
    public Result findHistoryArchiveList(@PathVariable("year") String year){
        return new Result(ResultCode.SUCCESS,archiveService.findHistoryArchiveList(year,this.companyId));
    }

    /**
     *
     * 微服务调用
     * 根据用户id和考勤年月查询用户考勤归档明细
     * /social_securitys/historys/data?userId=1063705482939731968&yearMonth=201906
     */
    @GetMapping("/historys/data")
    public Result findArchiveDetailListByYearMonthAndUserId(@RequestParam("userId") String userId,@RequestParam("yearMonth") String yearMonth){
        return new Result(ResultCode.SUCCESS,archiveService.findArchiveDetailListByYearMonthAndUserId(userId,yearMonth));
    }

}
